﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using _XNA__RunningDeath.GLO;

namespace _XNA__RunningDeath.GUI
{
    class CGUI_MenuMain:CDiaLogBox
    {
        #region contractor
        Texture2D _texBackgroundMain;
        Texture2D _texBtnNew;
        Texture2D _texBtnLoad;
        Texture2D _texBtnHighScore;
        Texture2D _texBtnExit;
        Texture2D _texBtnHelp;
        Texture2D _texBtnSetting;
        Texture2D _texBtnAbout;
        Texture2D _texBtnSoundOff;
        Texture2D _texBtnSoundOn;
        bool isVolumOn;

        #endregion

        public CGUI_MenuMain(ContentManager content, GameTime gameTime, SpriteBatch spriteBatch):base(content,gameTime,spriteBatch) 
        {
           if(content != null)
             {
                 this._texBackgroundMain =_content.Load<Texture2D>(@"GUI/bgMain");
                 this._texBtnNew = _content.Load<Texture2D>(@"GUI/btnNew");
                 this._texBtnLoad = _content.Load<Texture2D>(@"GUI/btnLoad");
                 this._texBtnHighScore = _content.Load<Texture2D>(@"GUI/btnHighscore");
                 this._texBtnHelp = _content.Load<Texture2D>(@"GUI/btnHelp");
                 this._texBtnSetting = _content.Load<Texture2D>(@"GUI/btnSetting");
                 this._texBtnExit = _content.Load<Texture2D>(@"GUI/btnExit");
                 this._texBtnSoundOff = _content.Load<Texture2D>(@"GUI/btnsoundOFF");
                 this._texBtnSoundOn = _content.Load<Texture2D>(@"GUI/btnSoundON");
                 this._texBtnAbout = _content.Load<Texture2D>(@"GUI/btnAbout");
                 this._texBG = this._content.Load<Texture2D>(@"GUI/guiBG");

                 this._dstoado = this._content.Load<Dictionary<string, Rectangle>>(@"GUI/ztoadoXMLgui");
                 isVolumOn = CGLOBAL.isVolumOn;
                 CGLOBAL.NewGame = enumGame.mNew;
             }

        }

        public override void Update(GameTime gameTime)
        {
            CGLOBAL.MousePrev = CGLOBAL.MouseCurrent;
            CGLOBAL.MouseCurrent = Mouse.GetState();

            if (CGLOBAL.MouseCurrent.LeftButton == ButtonState.Pressed && CGLOBAL.MousePrev.LeftButton == ButtonState.Released)
            {
                CGLOBAL.GUISelectedPrev = CGLOBAL.GUISelected;
                if (this._dstoado["btnAbout"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GUISelected = GUIenum.guiAbout;
                }
                else if (this._dstoado["btnSetting"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GUISelected = GUIenum.guiSetting;
                }
                else if (this._dstoado["btnNew"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GUISelected = GUIenum.guiRunningDeath;
                }
                else if (this._dstoado["btnLoad"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GUISelected = GUIenum.guiLoadGame;
                }
                else if (this._dstoado["btnHighScore"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GUISelected = GUIenum.guiHighScore;
                }
                else if (this._dstoado["btnHelp"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GUISelected = GUIenum.guiHelp;
                }
                else if (this._dstoado["btnExit"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.GUISelected = GUIenum.exit;
                }
                else if (this._dstoado["btnSoundON"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CThamSo.volumMusicPrev = CThamSo.volumMusic;
                    CThamSo.volumSoundPrev = CThamSo.volumSound;

                    CThamSo.volumMusic = 0f;
                    CThamSo.volumSound = 0f;
                   
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    isVolumOn = false;
                }
                else if (this._dstoado["btnSoundOFF"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CThamSo.volumSound = CThamSo.volumSoundPrev;
                    CThamSo.volumMusic = CThamSo.volumMusicPrev;
                    isVolumOn = true;
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound,CThamSo.pitchSound,CThamSo.panSound);
                }
            }
            //else
            //{
            //    isVolumOn = false;
            //    isVolumOff = false;
            //}
        }

        public override void Draw(GameTime gameTime)
        {
            this._spriteBatch.Begin();

            this._spriteBatch.Draw(this._texBackgroundMain,Vector2.Zero, Color.White);
            this._spriteBatch.Draw(this._texBtnNew, new Vector2(_dstoado["btnNew"].X, _dstoado["btnNew"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnLoad, new Vector2(_dstoado["btnLoad"].X, _dstoado["btnLoad"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnHighScore, new Vector2(_dstoado["btnHighScore"].X, _dstoado["btnHighScore"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnHelp, new Vector2(_dstoado["btnHelp"].X, _dstoado["btnHelp"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnAbout, new Vector2(_dstoado["btnAbout"].X, _dstoado["btnAbout"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnSetting, new Vector2(_dstoado["btnSetting"].X, _dstoado["btnSetting"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnExit, new Vector2(_dstoado["btnExit"].X, _dstoado["btnExit"].Y), Color.White);
            //this._spriteBatch.Draw(this._texBtnSoundOn, new Vector2(_dstoado["btnSoundON"].X, _dstoado["btnSoundON"].Y), Color.White);

            if (isVolumOn)
            {
                this._spriteBatch.Draw(this._texBtnSoundOn, new Vector2(_dstoado["btnSoundON"].X, _dstoado["btnSoundON"].Y), Color.White);
            }
            else{
                this._spriteBatch.Draw(this._texBtnSoundOff, new Vector2(_dstoado["btnSoundOFF"].X, _dstoado["btnSoundOFF"].Y), Color.White);
            }
            this._spriteBatch.End();
        }
    }
}
